import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hotpot/basic/app_colors.dart';
import 'package:hotpot/http/http_api.dart';
import 'package:hotpot/odoo/odoo_api.dart';
import 'package:hotpot/odoo/odoo_helper.dart';
import 'package:hotpot/route/route_ext.dart';
import 'package:hotpot/route/routes.dart';
import 'package:hotpot/utils/storage_utils.dart';
import 'package:hotpot/utils/user_utils.dart';
import 'package:hotpot/widgets/widget_ext.dart';
import 'package:hotpot/widgets/z_container.dart';

class DataPage extends StatefulWidget {
  const DataPage({super.key});

  @override
  State<DataPage> createState() => _DataPageState();
}

class _DataPageState extends State<DataPage> {
  var dbController = TextEditingController();
  var accountController = TextEditingController();
  var passwordController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (kDebugMode) {
      dbController.text = 'hotpot';
      accountController.text = 'admin';
      passwordController.text = 'admin';
    }
  }

  var obscureText = true;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Scaffold(
          backgroundColor: AppColors.baColor,
          body: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                54.hSizedBox,
                'images/img_logo.png'.assetImage(width: 141, height: 40),
                35.hSizedBox,
                TextField(
                  controller: accountController,
                  decoration: InputDecoration(
                    labelText: 'Usuario',
                    labelStyle: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
                  ),
                  style: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
                ).sizedBox(width: 298),
                32.hSizedBox,
                TextField(
                  obscureText: true,
                  controller: passwordController,
                  decoration: InputDecoration(
                    labelText: 'Contraseña',
                    labelStyle: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
                  ),
                  style: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
                ).sizedBox(width: 298),
                34.hSizedBox,
                ZContainer(
                  alignment: Alignment.center,
                  radius: 18,
                  width: 94,
                  height: 35,
                  color: const Color(0xFF22CE67),
                  child: 'INICIAR SESIÓN'.text(size: 10, color: Colors.white, bold: true),
                ).gestureDetector(onTap: _login),
              ],
            ),
          ).align(Alignment.topCenter),
        ),
        'images/img_back.png'
            .assetImage(width: 10, height: 15)
            .paddingOnly(left: 15, top: 16)
            .gestureDetector(onTap: () => Routes.loginService.popAndPushNamed()),
      ],
    );
  }

  void _login() async {
    if (accountController.text.isEmpty) {
      showToast('请输入账号');
      return;
    } else if (passwordController.text.isEmpty) {
      showToast('请输入密码');
      return;
    }
    try {
      SmartDialog.showLoading();
      var dbList = (await HttpApi.database()) as List;
      var db = dbList.first.toString();
      StorageUtils.saveLogin([db, accountController.text, passwordController.text]);
      var value = await OdooHelper.init(db, accountController.text, passwordController.text);
      SmartDialog.dismiss();
      if (value) {
        var partner = await OdooApi.getPartner(id: OdooHelper.partnerId);
        UserUtils.init(partner.first);
        loadData();
      } else {
        showToast('登录失败');
      }
    } catch (_) {
      SmartDialog.dismiss();
    }
  }

  void loadData() async {
    try {
      var config = await HttpApi.getPosConfigList();
      var shopList = config.result as List;
      Routes.store.popAndPushNamed(arg: {'data': shopList});
    } catch (_) {
      showToast('初始化失败');
    }
  }
}
